<?php
require_once('..\Database\DBCon.php');
require_once('OrderLocation.php');
require_once('OrderDetail.php');
class UserOrder
{
	//require_once('Database/DBCon.php');
	public $OrderLocation;
	public $OrderDetail;
	
	public function AddOrder()
	{
	$isOrder = true;
		$oId = mt_rand(100000,999999);
		//print count($this->OrderDetail);
		foreach($this->OrderDetail as $key=>$n){		
		$orderQuery = "INSERT INTO orderdetail(OrderId,MedicineName,Quantity,Type,Company,Power) VALUES('".$oId."','".$this->OrderDetail[$key]->MedicineName."','".$this->OrderDetail[$key]->Quantity."','".$this->OrderDetail[$key]->Type."','".$this->OrderDetail[$key]->Company."','".$this->OrderDetail[$key]->Power."');";
		//print 'Query is ' . $orderQuery;
		$result = mysql_query($orderQuery);
		//print 'Result is ' . $result;
		if($result)
		{
		$isOrder = true;
			
		} else
		{
		 $isOrder = false;
		}
		}
		if($isOrder)
		{
		//print 'Order has been added <br/>';
			$this->OrderLocation->OrderId = $oId;
			//print 'Order id is ' . $this->OrderLocation->OrderId;
			return $this->AddLocation($this->OrderLocation);
		} else
		{
		 return $isOrder;
		}
			
	}
	private function AddLocation($locDetail)
	{
	 if($locDetail->OrderId>0)
		{
			$qDetail ="INSERT INTO orderaddress(OrderId,Email,Mobile,Address) VALUES('".$locDetail->OrderId."','".$locDetail->EmailAddress."','".$locDetail->MobileNumber."','".$locDetail->Address."');";
			$resDetail = mysql_query($qDetail);
			if($resDetail)
			{
				return $this->UpdateOrderStatus($locDetail->OrderId,'0','1','0','Ordered');
				//$URL="../index.php?OnlineOrder&orderid=".$locDetail->OrderId;
				//echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">'; 
			//	print 'Order has been taken, Order Id is  ' . $locDetail->OrderId;
			} else
			{
			 return false;
			}
		} else
		{
		 return false;
		}
	}
	
	public function UpdateOrderStatus($orderId,$status,$updatedBy,$deliveredBy,$comment)
	{
		include('..\Common\Utilities.php');
		//$currDate = getdate();
		//print 'curr date is ' . GetDate::getCurrentDate(null);
		$qStatusUpdate ="INSERT INTO orderstatus(OrderId,OrderStatus,DeliveredBy,DeliveryDate,Comments) VALUES('".$orderId."','".$status."','".$deliveredBy."','".GetDate::getCurrentDateTime(null)."','".$comment."');";
		$resultUpdate = mysql_query($qStatusUpdate);
		//print $qStatusUpdate;
		if($resultUpdate)
		{
			return	true;
		} else
		{
		 return false;
		}
	}
}
?>